package com.common;

import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;

/**
 * response返回数据
 * <p>
 * Created by Administrator on 2018-8-20 0020.
 */
public class ServletWriteUtils {

    /**
     * 返回json系列化后的字符串
     *
     * @param response
     * @param object
     * @throws Exception
     */
    public static void responseJSONString(HttpServletResponse response, Object object) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter = response.getWriter();
        String jsonStr = JSON.toJSONString(object);
        printWriter.write(jsonStr);
        printWriter.flush();
    }

    /**
     * 返回字符串
     *
     * @param response
     * @param str
     * @throws Exception
     */
    public static void responseString(HttpServletResponse response, String str) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(str);
        printWriter.flush();
    }

}
